package com.qzsoft.zhjd.asphalt.modules.asphaltalarm.service.impl;

import java.util.HashMap;
import java.util.Map;

import com.qzsoft.zhjd.asphalt.modules.asphaltalarm.mapper.AsphaltAlarmMapper;
import com.qzsoft.zhjd.asphalt.modules.asphaltalarm.service.AsphaltAlarmService;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.asphalt.modules.asphaltalarm.dto.AsphaltAlarmCountDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltalarm.dto.AsphaltAlarmHandlerDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltalarm.dto.AsphaltAlarmHandlerEditDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltalarm.dto.AsphaltAlarmListDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltalarm.entity.AsphaltAlarmEntity;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;

/**
 * @ClassName AsphaltAlarmServiceImpl
 * @Description 预警信息业务接口实现
 * @author linhuawei
 * @since 2021-11-26
 */
@Service
public class AsphaltAlarmServiceImpl extends ServiceImpl<AsphaltAlarmMapper, AsphaltAlarmEntity>
        implements AsphaltAlarmService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @ZhjdDataFilter(tableAlias = "t2")
    @Override
    public IPage<AsphaltAlarmListDto> pageDto(Map<String, Object> params) {
        // 判断是否设置预警类型
        if (ObjectUtils.isEmpty(params.get("alarmType"))) {
            params.put("alarmType", "all");
        }

        IPage<AsphaltAlarmListDto> page = baseServiceComponent.getPage(params, AsphaltAlarmListDto.class);
        return baseMapper.queryDto(page, params);
    }

    @ZhjdDataFilter(tableAlias = "t2")
    @Override
    public AsphaltAlarmCountDto countDto(Map<String, Object> params) {
        if(null == params) {
            params = new HashMap<String, Object>();
        }
        return baseMapper.countDto(params);
    }

    @Override
    public AsphaltAlarmHandlerDto handler(String id) {
        return baseMapper.queryHandlerDto(id);
    }

    @Override
    public void handlerEdit(AsphaltAlarmHandlerEditDto entity) {
        UpdateWrapper<AsphaltAlarmEntity> updateWrapper = new UpdateWrapper<AsphaltAlarmEntity>();
        updateWrapper.eq("id", entity.getId());
        updateWrapper.set("alarm_reason", entity.getAlarmReason());
        updateWrapper.set("handler_plan", entity.getHandlerPlan());
        updateWrapper.set("handler_result", entity.getHandlerResult());
        updateWrapper.set("handler_time", entity.getHandlerTime());
        updateWrapper.set("handler_person",entity.getHandlerPerson());
        updateWrapper.set("state", "1");
        baseMapper.update(null, updateWrapper);
    }

}
